# -*- coding: utf-8 -*-
"""
兼容不同的excel版本,并读写excel文件
"""
import os
from common import OldExcel
from common import NowExcel

def get_reader(srcfile=''):
    """
    获取读取excel的对象
    :param srcfile: excel文件路径
    :return: 读取excel的对象
    """
    reader = None

    # 如果打开的文件不存在,就报错
    if not os.path.isfile(srcfile):
        # print("%s not exist!" % (srcfile))
        return reader

    if srcfile.endswith('.xls'):
        reader = OldExcel.Reader()
        reader.open_excel(srcfile)
        return reader

    if srcfile.endswith('.xlsx'):
        reader = NowExcel.Reader()
        reader.open_excel(srcfile)
        return reader

def get_writer(srcfile, dstfile):
    """
    获取写入excel的对象
    :param srcfile: excel远程文件路径
    :param dstfile: 写入excel文件保存的路径
    :return: 写入excel的对象
    """
    writer = None

    # 如果打开的文件不存在,就报错
    if not  os.path.isfile(srcfile):
        print("% not exist!" % (srcfile))
        return writer

    if srcfile.endswith('.xls'):
        writer = OldExcel.Writer()
        writer.copy_open(srcfile, dstfile)
        return writer

    if srcfile.endswith('.xlsx'):
        writer = NowExcel.Writer()
        writer.copy_open(srcfile, dstfile)
        return writer

if __name__ == '__main__':
    reader = get_reader('../lib/ExcelFile/shopxo项目用例.xlsx')
    sheetname = reader.get_sheets()

    for sheet in sheetname:
        reader.set_sheet(sheet)
        lines = reader.readline()

        for i in range(len(lines)):
            print(lines[i])

        print()

    writer = get_writer('../lib/ExcelFile/shopxo项目用例.xlsx', '../lib/ExcelFile/result-shopxo项目用例.xlsx')
    sheetnamew = writer.get_sheets()
    writer.set_sheet(sheetnamew[0])
    writer.write(1,1, 'hby', 3)
    writer.save_close()

